home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / T_BCINFO.ARJ / TI805.ASC < prev    next >
Text File  |  1992-02-25  |  3KB  |  133 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.   PRODUCT  :  Turbo C++                              NUMBER  :  805
  8.   VERSION  :  All
  9.        OS  :  DOS
  10.      DATE  :  February 25, 1992                        PAGE  :  1/2
  11.  
  12.     TITLE  :  Member Data Initialization -- Using a Constructor
  13.               Initialization List
  14.  
  15.  
  16.  
  17.  
  18.   The following code demonstrates the initialization of a class's
  19.   member data by using the initialization list of a class
  20.   constructor.  It also shows how to call a base class constructor
  21.   from a derived class.
  22.  
  23.   Since this code uses floating point, be sure to have floating
  24.   point emulations enabled (IDE) or include EMU.LIB and MATHx.LIB
  25.   at the appropriate location in the TLINK command line.
  26.  
  27.   #include <iostream.h>
  28.   #define HUND 100
  29.  
  30.   class base {
  31.     public:
  32.       base() { cout << "in base\n"; }
  33.   };
  34.  
  35.   class derive : public base {
  36.       int x, z;
  37.       float y;
  38.  
  39.     public:
  40.       /* Note that the initialization list comes AFTER the
  41.          contructors formal arguments and a colon. Each
  42.          variable name is followed by an initial value in
  43.          parentheses.  Each variable is separated from the
  44.          next by a comma.  The value in the parentheses can
  45.          be either a variable from the constructor parameter
  46.          list, a value, an expression that results in a value,
  47.          or a constant.
  48.  
  49.          At the end of the initialization list is the call to
  50.          the base class constructor.  In this example, parameters
  51.          are not passed, however, parameters may be passed and
  52.          they may consist of a variable from the derived
  53.          constructor parameter list, a value, an expression
  54.          that results in a value, or a constant.
  55.  
  56.          Note: a private or public member of the base class
  57.                cannot be initialized by the initialization list
  58.                of the derived constructor.
  59.       */
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.   PRODUCT  :  Turbo C++                              NUMBER  :  805
  74.   VERSION  :  All
  75.        OS  :  DOS
  76.      DATE  :  February 25, 1992                        PAGE  :  2/2
  77.  
  78.     TITLE  :  Member Data Initialization -- Using a Constructor
  79.               Initialization List
  80.  
  81.  
  82.  
  83.  
  84.   // initialization list Call to base constructor
  85.   derive(int a) : x(a*a), z(HUND), y(4.33), base()
  86.   {
  87.       cout << "in derive\n";
  88.   }
  89.  
  90.   void show(void)
  91.   {
  92.       cout << "x is " << x;
  93.       cout << "\nz is " << z;
  94.       cout << "\ny is " << y;
  95.   };
  96.  
  97.   main()
  98.   {
  99.       derive dd(3);
  100.       dd.show();
  101.   }
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.